package polyn;

public class LagrangePolyn implements PolynomialFunction {

    private final int degree;
    private final Pair[] points;

    public LagrangePolyn(Pair[] points) {
        this.points = points;
        degree = PolynFamily.degree;
    }

    @Override
    public double coeff(int j, double X) {
        double ret = 1;
        for (int i = 0 ; i < degree ; i++) {
            if (i != j) {
                ret *= (X - points[i].x) / (points[j].x - points[i].x);
            }
        }
        return ret;
    }

    @Override
    public double L(double X) {
        double ret = 0;
        for (int j = 0 ; j < degree ; j++) {
            ret += points[j].y * coeff(j, X);
        }
        return ret;
    }
}
